package com.learn.app.user.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.learn.util.StringUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import com.learn.service.dm.UserInfoDM;
import com.learn.service.webservice.IUserService;
import com.learn.tools.spring.SpringBeanTool;

@Controller
@RequestMapping("/user")
public class UserInfoController extends BaseController {

    private Logger logger = LoggerFactory.getLogger(UserInfoController.class);

    /**
     * 调用服务端webservice接口查询用户数据
     *
     * @author:  liangan
     * @version  1.0.0
     */
    @RequestMapping("/userList")
    public String findUserList(final HttpServletRequest request,final HttpServletResponse response,final Model model,@ModelAttribute("userInfoDM") final UserInfoDM dm) {
        List<UserInfoDM> tmpList = getUserService().getUserList(dm);
        model.addAttribute("userInfoListDM", tmpList);
        return "userList";
    }

    /**
     * 调用服务端webservice接口，根据用户ID返回用户详情数据
     *
     * @author:  liangan
     * @version  1.0.0
     */
    @RequestMapping("/getUser")
    public String getUserById(final HttpServletRequest request,final HttpServletResponse response,final Model model,@ModelAttribute("userId") final String userId) {
        UserInfoDM userInfoDM = getUserService().getUserById(Integer.parseInt(userId));
        model.addAttribute("userDm", userInfoDM);
        return "userDetail";
    }

    /**
     * 调用服务端webservice接口，根据用户ID删除用户
     *
     * @author:  liangan
     * @version  1.0.0
     */
    @RequestMapping("/delUser")
    public String delUser(final HttpServletRequest request,final HttpServletResponse response,final Model model,@ModelAttribute("userId") final String userId) {
        int resp = getUserService().delUserById(Integer.parseInt(userId));
        if (1 == resp){
            logger.info("用户[{}]删除成功", userId);
        }else{
            logger.info("用户[{}]删除失败", userId);
        }
        //重定向用户列表页
        return "redirect:/user/userList";
    }

    /**
     * 跳转到新增用户页
     *
     * @author:  liangan
     * @version  1.0.0
     */
    @RequestMapping("/toAddUser")
    public String toAddUser(final HttpServletRequest request,final HttpServletResponse response,final Model model) {
        return "addUser";
    }

    /**
     * 调用服务端webservice接口，新增用户
     *
     * @author:  liangan
     * @version  1.0.0
     * @return
     */
    @RequestMapping("/addUser")
    public String addUser(final HttpServletRequest request,final HttpServletResponse response,final Model model,@ModelAttribute("userInfoDM") final UserInfoDM userInfoDM) {
        if(null != userInfoDM && !StringUtil.isBlank(userInfoDM.getUserName())){
            int resp = getUserService().addUser(userInfoDM);
            if (1 == resp){
                logger.info("用户[{}]保存成功", userInfoDM.getUserName());
                //用户保存成功，重定向用户列表页
                return "redirect:/user/userList";
            }else{
                logger.info("用户[{}]保存失败", userInfoDM.getUserName());
            }
        }
        // 用户保存失败，保留在用户新增页
        return "addUser";
    }


    private IUserService getUserService() {
        return (IUserService) SpringBeanTool.getBean("userService");
    }

}
